package com.multidisplay.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.multidisplay.bo.ResultBo;
import com.multidisplay.mapper.UserMapper;
import com.multidisplay.po.User;
import com.multidisplay.pojo.AddUser;

import com.multidisplay.service.impl.UserServiceImpl;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@CrossOrigin
@Api(tags = "用户管理相关接口")
public class UserController {
    @Resource
    private UserServiceImpl userService;
    @Resource
    private UserMapper userMapper;

    @GetMapping("/userfindAll")
    public ResultBo<User> findAll(int page, int limit){
        ResultBo<User> all = userService.findAll(page, limit);
        return all;
    }

    @RequestMapping(value = "/userfindByuId")
    @ResponseBody
    public ResultBo<User> findByuId(@RequestParam(name = "_uId",required = false) String _uId,int page,int limit){
        if (_uId=="") {
            ResultBo<User> resultBo = userService.findAll(page, limit);
            return resultBo;
        }
           QueryWrapper<User> queryWrapper = new QueryWrapper<>();
           queryWrapper.eq("u_id",_uId);
           IPage iPage = new Page(page, limit);
           IPage<User> userIPage = userMapper.selectPage(iPage, queryWrapper);
           ResultBo<User> resultBo = new ResultBo<>();
           resultBo.setCode(0);
           resultBo.setMsg("");
           resultBo.setCount(userIPage.getTotal());
           resultBo.setData(userIPage.getRecords());
           return resultBo;
    }

    @RequestMapping(value = "/useradd")
    @ResponseBody
    public ResultBo<User> addUser(AddUser addUser){
        ResultBo<User> resultBo=new ResultBo<>();
        int i=userService.addUser(addUser);
        if (i==1){
            resultBo.setMsg("success");
            return resultBo;
        }
        else if (i==-1){
            resultBo.setMsg("exist");
            return resultBo;
        }
        else {
            resultBo.setMsg("false");
            return resultBo;
        }
    }

    @PostMapping("/userdelete")
    public void deleteUser(String uId){
        userService.deleteUser(uId);
    }

    @RequestMapping(value="/userupdate")
    @ResponseBody
    public ResultBo<User> updateUser(User user){
        System.out.println("\n\n\n\n"+user.toString());
        ResultBo<User> resultBo = new ResultBo<>();
        resultBo.setCode(0);
        resultBo.setCount(0);
        if(userService.updateUser(user))
          resultBo.setMsg("1");
        else
            resultBo.setMsg("0");
        return resultBo;
    }

}
